package com.modules.bluetooth;//指示这个类的包所属

import android.widget.Toast;//用来打印
//蓝牙适配器
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

//导入react封装需要的库
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
//用来得到从javascript得到的array的
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.uimanager.IllegalViewOperationException;

//导入java要用的工具
import java.util.HashMap;
import java.util.Map;

import java.util.Set;
import java.util.Iterator;

import java.util.UUID;
import java.io.IOException;
import java.io.OutputStream;

public class BluetoothModule extends ReactContextBaseJavaModule {

	//取得默认的蓝牙适配器 //调用方法获取蓝牙适配器
	private BluetoothAdapter _mybluetooth= BluetoothAdapter.getDefaultAdapter();
	private BluetoothSocket _socket = null;
    private Set<BluetoothDevice> _devices=null;//与之配对的蓝牙设备列表
    private BluetoothDevice _remotedev=null;//要操作的配对蓝牙设备
    private OutputStream _myOutputStream=null;//写通道

	//构造函数
	public BluetoothModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }
    //在javascript中的命名
    @Override
    public String getName() {
        return "BluetoothAndroid";
    }

    //暴露到react的方法，打开蓝牙适配器
    @ReactMethod
    public void enable() {
    	_mybluetooth.enable();
    	//toast显示信息
        Toast.makeText(getReactApplicationContext(),"open bluetooth", Toast.LENGTH_SHORT).show();
    }

    //暴露到react的方法，关闭蓝牙适配器
    @ReactMethod
    public void disable() {
    	_mybluetooth.disable();
    	//toast显示信息
        Toast.makeText(getReactApplicationContext(),"disable bluetooth", Toast.LENGTH_SHORT).show();
    }

/*
    @ReactMethod
    public void startDiscovery() {
    	_mybluetooth.startDiscovery();
    	//toast显示信息
        Toast.makeText(getReactApplicationContext(),"disable bluetooth", Toast.LENGTH_SHORT).show();
    }*/

    //得到所有已经配对的蓝牙设备
    @ReactMethod
    public void getBondedDevices() {
    	_devices = _mybluetooth.getBondedDevices();
    	if(_devices.size()>0){
    		//使用迭代器遍历
    		for(Iterator iterator = _devices.iterator();iterator.hasNext();){
    			_remotedev=(BluetoothDevice)iterator.next();
    		};  		
    	};
    }

    //socket连接这种情况是远端设备作为server
    //运行前提：1.蓝牙已经打开并配对
    //		   2._remotedev不为空，函数getBondedDevices会对其赋值
    //         3.远端设备开启了server服务
    //做的事情：1.进行socket连接
    //		  2.对io读写端口_myOutputStream进行赋值
    @ReactMethod
    public void socketconnet() {
    	//这个uuid比较通用
    	final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
    	UUID uuid = UUID.fromString(SPP_UUID);
    	if(_remotedev==null){
    		return;
    	}
    	//创建socket
		try{
			_socket = _remotedev.createInsecureRfcommSocketToServiceRecord(uuid);
			//如果没有连接
			if(!_socket.isConnected()){
				//连接
				_socket.connect(); 
			}
			//对读写通道进行赋值
			try{
				_myOutputStream = _socket.getOutputStream();
			}catch(IOException e){
				Toast.makeText(getReactApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show();
			}

		}catch(IOException e){
			//toast显示信息
			Toast.makeText(getReactApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show();
		}finally{

		}
    }
    //关闭socket连接
    @ReactMethod
    public void socketclose() {
    	if (_socket != null) {
			try {
				Toast.makeText(getReactApplicationContext(),"socket close", Toast.LENGTH_SHORT).show();
				_socket.close();
			} catch (IOException e) {

			}
		}
	}
    //socket发送数据
    @ReactMethod
    public void socketwrite(ReadableArray arry) {
    	byte[] sendbytes=new byte[arry.size()];
    	for(int i=0;i<arry.size();i++){
    		sendbytes[i]=(byte) arry.getInt(i);
    	}

    	Toast.makeText(getReactApplicationContext(),"socket write", Toast.LENGTH_SHORT).show();
    	try{
    		//_myOutputStream.write(0xfe);
    		_myOutputStream.write(sendbytes);
    	}catch(IOException e){
			//toast显示信息
			Toast.makeText(getReactApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show();
    	}

    }

}